package Q905

import java.lang.StringBuilder

/**
 * 按奇偶排序数组
 * @author 23737
 * @date 2022.4.28
 */
class Main {

}

fun main() {
    val nums : IntArray = intArrayOf(3,1,2,4)
    var sortArrayByParity = Solution().sortArrayByParity(nums)
    var sortArrayByParity2 = Solution().sortArrayByParity2(nums)
    for(i in sortArrayByParity){
        print(i )
    }
    println()
    for (i in sortArrayByParity2) {
        print("$i  ")
    }
}


class Solution {
    fun sortArrayByParity(nums: IntArray): IntArray {
        val res: MutableList<Int> = ArrayList()
        val jiNumber: MutableList<Int> = ArrayList()
        val ouNumber: MutableList<Int> = ArrayList()
        for (i in nums) {
            if (i % 2 != 0) {
                jiNumber.add(i)
            } else {
                ouNumber.add(i)
            }
        }
        for(i in ouNumber){
            res.add(i)
        }
        for(i in jiNumber){
            res.add(i)
        }
        return res.toIntArray()
    }

    fun sortArrayByParity2(nums:IntArray) : IntArray{
        val n = nums.size
        val res : IntArray = IntArray(n)
        var index = 0
        for(i in nums){
            if (i % 2 == 0){
                res[index++] = i
            }
        }
        for(i in nums){
            if(i % 2 != 0){
                res[index++] = i
            }
        }
        return res
    }
}